<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<style>
    .el-input-item {
        width: 260px;
    }

</style>
<div id="app">
    <template>
        <el-form :model="formData" ref="formData" :rules="rules" label-width="100px"
                 style="margin-top: 5%;display: flex;flex-direction: row;align-items: center;flex-wrap: wrap;justify-content: space-between;margin-right: 5%">
            <el-form-item label="名字" prop="name">
                <el-input class="el-input-item" size="small" v-model="formData.name"
                          placeholder="请输入名字"/>
            </el-form-item>
            <el-form-item label="作者" prop="author">
                <el-input class="el-input-item" size="small" v-model="formData.author"
                          placeholder="请输入作者"/>
            </el-form-item>
            <el-form-item v-if="formData.type!=3" label="图片" prop="picture">
                <el-upload class="avatar-uploader el-input-item" :action="uploadUrl" :headers="uploadHeaders"
                           :data="uploadData"
                           accept=".jpg,.png,.jpeg,.gif"
                           :show-file-list="true"
                           :on-success="handleAvatarSuccess.bind(this,'head')" :before-upload="beforeAvatarUpload">
                    <img v-if="formData.picture" :src="formData.picture" class="avatar"
                         style="width:200px;height: 200px">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="发布时间" prop="publishDate">
                <el-date-picker clearable size="small" style="width: 260px"
                                v-model="formData.publishDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择发布时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="分类">
                <el-select v-model="formData.classId" placeholder="请选择分类" class="el-input-item"
                           size="small">
                    <el-option
                            v-for="dict in classIdOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="parseInt(dict.dictValue)"
                    ></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div style="text-align: center;margin-top: 5%">
            <el-button size="small" @click="closePage">取 消</el-button>
            <el-button size="small" type="primary" @click="handleSubmit">确 定</el-button>
        </div>
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/admin/sysBook.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    let id = getQueryString("id");
    new Vue({
        el: '#app',
        data() {
            return {
                dialogVisible: false,
                formData: {},
                uploadUrl: '/common/upload/file',
                uploadHeaders: '',
                uploadData: {'module': '1'},
                // 分类字典
                classIdOptions: [],
                rules: {
                    createTime: [
                        {required: true, message: "创建时间不能为空", trigger: "blur"}
                    ],
                }
            }
        },
        created() {
            if (id) {
                detail(id).then((res) => {
                    if (res.code === '200') {
                        this.formData = res.data
                    }
                });
            } else {
                this.formData = {}
            }
            getDicts("book_class").then(response => {
                this.classIdOptions = response.data;
            });

        },
        methods: {
            handleAvatarSuccess(idx, res, file, name) {
                if (res.code === '200') {
                    console.log(res);
                    this.formData.picture = res.data;
                    this.uploadData = [{picture: res.data}]
                }
            },
            handleAvatarSuccessIndex(idx, res, file, name) {
                if (res.code === '200') {
                    console.log(res);
                    this.formData.urlIndex = res.data;
                    this.uploadData = [{urlIndex: res.data}]
                }
            },
            beforeAvatarUpload(file) {
                let isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过2MB!');
                }
                return isLt2M;
            },
            handleSubmit() {
                if (id) {
                    addOrUpdate('/admin/sysBook/update', this);
                } else {
                    addOrUpdate('/admin/sysBook/add', this);
                }
            },

        }
    });
</script>
</html>
